package com.lll.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lll
 * @Title: FileUpload
 * @Description: 文件上传
 * 访问 http://localhost:8182/upload 即可实现文件上传
 * 上传至F:\Development\Github_Project\springBoot-study\springboot-fileUpload
 * @Version:1.0.0
 * @date 2018年5月11日
 */
@Controller
public class FileUploadController {

    @Value("${filePath}")
    private String filePath;

    @GetMapping("/upload")
    public String uploading() {
        //跳转到 templates 目录下的 uploading.html
        return "uploading";
    }

    //处理文件上传
    @PostMapping("/uploading1")
    public @ResponseBody String uploading1(@RequestParam("file") MultipartFile file,
            HttpServletRequest request) {
        String fileName = file.getOriginalFilename();
        String fileExtension = fileName.substring(fileName.lastIndexOf("."));
        //String targetFilePath = new StringBuilder().append("/tmp/").append(UUID.randomUUID()).append('_').append(fileName)
        //      .toString();
        try {
            FileOutputStream fos = new FileOutputStream(new File("temp" + UUID.randomUUID() + fileExtension));
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            bos.write(file.getBytes());
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("文件上传失败!");
            return "uploading failure";
        } finally {
        }
        System.out.println("文件上传成功!");
        return "uploading success";
    }

    public void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath + fileName);
        out.write(file);
        out.flush();
        out.close();
    }

    @PostMapping("/uploading")
    public @ResponseBody String uploading(@RequestParam("file") MultipartFile file,
            HttpServletRequest request) {
        try {
            String sourceName = file.getOriginalFilename();
            // 指定上传文件夹路径
            String targetFile = "D:/tmp/";
            // 上传全路径
            File tempTargetFile = new File(targetFile, UUID.randomUUID().toString() + sourceName.substring(sourceName.lastIndexOf(".")));
            // 查看路径是否存在，不存在就创建，这块是创建directory，文件夹D:/tmp/
            if (!tempTargetFile.getParentFile().exists()) {
                tempTargetFile.getParentFile().mkdirs();
            }
            //spring的transferTo保存文件方法，必须指定一个文件而不是文件夹，否则报错
            file.transferTo(tempTargetFile);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("文件上传失败!");
            return "uploading failure";
        } finally {
        }
        System.out.println("文件上传成功!");
        return "uploading success";
    }

}
